import groovy.xml.MarkupBuilder

suffix = '.groovy'
gwcScriptRoot = context.getRealPath('/chat')
gwcDomainRoot = context.getRealPath('/WEB-INF/src')

scripts = []
gwcScriptRootDir = new File(gwcScriptRoot)
gwcScriptRootDir.eachFileRecurse {file ->
	scripts << file
}

html.html {
	head {
		title('Liste des fichiers')
		link(rel : 'stylesheet', type : 'text/css', href : context.contextPath + '/js/ext-2.0/resources/css/ext-all.css')
		link(rel : 'stylesheet', type : 'text/css', href : context.contextPath + '/js/ext-2.0/resources/css/xtheme-gray.css')
		style(type : 'text/css', '''
			#tree {
		    	float:left;
		    	margin:20px;
		    	border:1px solid #c3daf9;
		    	width:250px;
		    	height:300px;
		    }
		''')
		script(type : 'text/javascript', src: context.contextPath + '/js/ext-2.0/adapter/ext/ext-base.js', '')
		script(type : 'text/javascript', src: context.contextPath + '/js/ext-2.0/ext-all-debug.js', '')
		script(type : 'text/javascript', src: context.contextPath + '/js/index.js', '')
		script(type : 'text/javascript', src: context.contextPath + '/js/Ext.ux.codepress.js', '')
	}
	body {
		div (id : 'north') {
			h1 ('Groovy Web Chat Builder')
		}

		if (request.getParameter('action') == 'open') {
			form (action : "index$suffix", method : 'post') {
				div {
					textarea(cols : 80, rows : 30, name : 'fileContent', new File(gwcScriptRoot, request.getParameter('fileName') + suffix)?.text)
				}
				div {
					input (type : 'hidden', name : 'fileName', value : request.getParameter('fileName'))
					input (type : 'hidden', name : 'action', value : 'save')
					input (type : 'submit', value : 'Modifier')
				}
			}
		}

		if (request.getParameter('action') == 'save') {
			def file = new File(contextRoot, request.getParameter('fileName') + suffix)
			if (!(file && file.delete()) || file.createNewFile())  {
				file << request.getParameter('fileContent')
			}

			div('Modification avec succès')
		}


		div (id : 'west', '')
	}
}
